var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentid",
            rootPId: 0
        },
        key: {
            url:"nourl",
            name:"classname"
        }
    }
};
var ztree;

$(function () {
	vm.getAppclass();
	var id = T.p('id');
	console.log(id);
    if(id == null){
        return ;
    }
	vm.getAppinfo(id);
});

var vm = new Vue({
    el: '#rrapp1',
    id: null,
    data: {
        appinfo: {
        	appname: null,
        	appclass: 1,
        	appclassName: null,
        	appstatus : 1,
        	callbackurl: null,
        	approoturl:null,
        	appconfigurl: null,
        	ordernum : 0
        }
    },
    methods: {
        add: function () {
        	vm.appinfo.id = T.p('id');
            $.ajax({
                type: "POST",
                url: baseURL + "sys/appinfo/save",
                contentType: "application/json",
                data: JSON.stringify(vm.appinfo),
                success: function (r) {
                    if (r.code == 0) {
                        alert('操作成功', function (index) {
                        	parent.location.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        saveOrUpdate: function () {
        	var id = T.p('id');
        	vm.getAppinfo(id);
            var url = vm.appinfo.id == null ? "sys/appinfo/save" : "sys/appinfo/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.appinfo),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                        	parent.location.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getAppinfo: function(id){
            $.get(baseURL + "sys/appinfo/info/"+id, function(r){
                vm.appinfo = r.appinfo;
                console.log(vm.appinfo);
            });
        },
        getAppclass: function(){
            //加载部门树
            $.get(baseURL + "sys/appclass/select", function(r){
                ztree = $.fn.zTree.init($("#appclassTree"), setting, r.appclassList);
                var node = ztree.getNodeByParam("id", vm.appinfo.appclass);
                if(node != null){
                    ztree.selectNode(node);
                    vm.appinfo.appclassName = node.classname;
                }
            })
        },
        openTree: function(){
        	//vm.getAppclass();
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择类别",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#appclassLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //alert(node[0].id);
                    //选择上级部门
                    vm.appinfo.appclass = node[0].id;
                    vm.appinfo.appclassName = node[0].classname;

                    layer.close(index);
                }
            });
        }
    }
});

